import 'package:earclean/app/request/http_request.dart';
import 'package:earclean/app/routes/app_pages.dart';
import 'package:earclean/app/util/Yue_utils.dart';
import 'package:earclean/app/util/common_utils.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

class HomeController extends GetxController {
  //TODO: Implement HomeController

  final bannerList = [].obs;
  final tuijianList = [].obs;
  final shushiList = [].obs;
  final showAddress = false.obs;

  openSet() async {
    await openAppSettings();
  }

  @override
  void onInit() {
    requestPermission();
    loadData();
    super.onInit();
  }

  loadData() async {
    final result = await HttpRequest.get('https://iamz.jiliol.net/caier.json');
    if (result.result) {
      List map = result.data ?? [];
      if (map.isNotEmpty) {
        bannerList.value = map[0]['list'] ?? [];
        tuijianList.value = map[1]['list'] ?? [];
        shushiList.value = map[2]['list'] ?? [];
      }
    }
  }

  void requestPermission() async {
    String mobile = box.read('mobile') ?? "";
    String oneKey = box.read('oneKey') ?? "";

    if (oneKey != '' || (mobile != '' && mobile != '15472626527')) {
      channel.invokeMethod('startLocation');
    } else {
      showAddress.value = false;
    }
  }

  //去详情
  toInfo(item) {
    Get.toNamed(Routes.INFO, arguments: {'id': item['id']});
  }

  //  服务详情
  toServiceInfo(item) {}

  // 预约
  toYue(Map item) {
    YueUtils.yuyue(item);
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {}
}
